home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 19 / Mac Magazin and MacEasy Magazine CD - Issue 19.iso / Wissenschaft & Technik / FFTs for RISC 1.1 / rfftTest.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  2KB  |  107 lines

  1. /*  A program to test and time real input fast fourier transform routine    */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include "fftlib.h"
  7.  
  8. #define    MACTIMING 1        /* true means time fft with macintosh calls    */
  9. #if MACTIMING
  10. #include <timer.h>
  11. #endif
  12.  
  13. #define    NUMROWS 1        /* process Matrix of NUMROWS different ffts of length N    */
  14. #define N 2048            /* size of FFT, must be a power of 2 */
  15. #define NTIMES 20        /* number of timing loops, invalid if too big (if a[0][0] = 0 | nan)*/
  16.  
  17. void main(){
  18. float        *Utbl;
  19. float        (*a)[N];
  20.  
  21. #if MACTIMING
  22. UnsignedWide         TheTime1;
  23. UnsignedWide         TheTime2;
  24. double        TheTime;
  25. #endif
  26.  
  27. long         i, il;
  28. long         TheErr;
  29. long        M;
  30.  
  31. Utbl = (float *) malloc((N/4+1)*sizeof(float));
  32. if (Utbl==0)
  33.     TheErr = 2;
  34. else
  35. TheErr = rFFTInit(&M, N, Utbl);
  36.  
  37. if(!TheErr){
  38.     a = (float (*)[N]) malloc(NUMROWS*N*sizeof(float));
  39.     if (a == 0) TheErr = 2;
  40. }
  41.  
  42. if(!TheErr){
  43.  
  44.             /*  set up a simple test case */
  45.     for (il=0; il<NUMROWS; il++){
  46.         for (i=0; i<N; i+=2){
  47.             a[il][i] = sqrt(il+i+.77777);    
  48.             a[il][i+1] = (il+i+.22222)*(il+i+.22222) / N - N/2;    
  49.         }
  50.         a[il][0] = N+3;
  51.         a[il][2] = 1-N;
  52.     }
  53.  
  54. #if MACTIMING
  55.     Microseconds(&TheTime1);
  56.     for (i=0;i<NTIMES;i++){        /* do many times for timing */
  57.         rffts((float *)a, M, NUMROWS, Utbl);
  58.     }
  59.     Microseconds(&TheTime2);
  60.  
  61.     TheTime = (double)(TheTime2.hi - TheTime1.hi) * 65536.0 * 65536.0;
  62.     TheTime = (TheTime + (double)(TheTime2.lo - TheTime1.lo))/1000.0;
  63.     printf("Time rfft = %12f   ms.    a[0][0]= %6e\n", TheTime/NTIMES/NUMROWS, a[0][0]);
  64.  
  65. #else
  66.     printf("start timing \n");
  67.     for (i=0;i<NTIMES;i++){        /* do many times for timing */
  68.         rffts((float *)a, M, NUMROWS, Utbl);
  69.     }
  70.     printf("end timing \n");
  71. #endif
  72.  
  73.     printf("\n");
  74.             /*  set up a simple test case */
  75.     for (il=0; il<NUMROWS; il++){
  76.         for (i=0; i<N; i+=2){
  77.             a[il][i] = sqrt(il+i+.77777);    
  78.             a[il][i+1] = (il+i+.22222)*(il+i+.22222) / N - N/2;    
  79.         }
  80.         a[il][0] = N+3;
  81.         a[il][2] = 1-N;
  82.     }
  83.  
  84.     rffts((float *)a, M, NUMROWS, Utbl);
  85.  
  86.     if (N*NUMROWS <= 512){
  87.          for (il=0; il<NUMROWS; il++){
  88.             printf("atrans = [ \n");
  89.             for (i=0; i<N; i+=2)
  90.                 printf(" %+20.15e + j * ( %+20.15e ) \n", a[il][i], a[il][i+1]);
  91.             printf("]; \n");    
  92.             }
  93.         }
  94.     else {  /* abbreviate big output */
  95.         printf("the last 20 values of the first transform are: \n");
  96.         for (i=N-40; i<N; i+=2)
  97.             printf(" %+20.15e + j * ( %+20.15e ) \n", a[0][i], a[0][i+1]);
  98.     }
  99.     free (a);
  100.     free (Utbl);
  101. }
  102. else
  103. if(TheErr==2)    printf(" out of memory ");
  104. else    printf(" error ");
  105. return;
  106. }
  107.